import { uDb, cline } from 'sui';

const load_supply = (mod, setMain, tp="supply", gtp="group", setTag) => {
    uDb(null, `file/list/${tp}`, { line: cline() },
        supply => {
            const map = {};
            const tag = {};

            supply.forEach(d => {
                map[d.sn] = d.name;

                if (d.tag) {
                    tag[d.sn] = d.tag;
                }
            });

            if (setTag) {
                setTag(tag);
            }

            uDb(null, `file/get/${gtp}`, { line: cline(), sn: mod },
                group => {
                    const set = new Set();
                    const ret = [];

                    if (group.user) {
                        group.user.forEach(d => set.add(d));
                    }

                    supply.forEach(d => {
                        if (set.has(d.sn)) {
                            ret.push({ id: d.sn, sn: d.sn, name: d.name });
                        }
                    });

                    setMain(ret);
                },
                e => setMain(supply));
        },
        e => setMain(null));
};

const load_lub = (setMain, setTag) => load_supply("lub", setMain, "stuff", "gstuff", setTag);

export {load_lub};
